#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "struct.h"
#include "methods.h"

/**************************
**		全局变量          **
**      字符常量          **
**     函数指针数组        **
**************************/
GtkWidget *win = NULL;
GtkWidget *win_building = NULL;
GtkWidget *win_classroom = NULL;
GtkWidget *win_arrangement = NULL;
GtkListStore *list_building = NULL;
GtkListStore *list_building_detail = NULL;
GtkListStore *list_classroom_detail = NULL;
GtkListStore *list_arrangement_detail = NULL;
GtkTreeIter treeiter;

FILE *fp = NULL;

Building *head = NULL;

Building *menu_building = NULL;
Classroom *menu_classroom = NULL;
Arrangement *menu_arrangement = NULL;

const char name[] = "教室排课信息系统";

const char *author[] = {"彭佳伟", "华中科技大学 2014级"};

const char version[] = "version 2";

const char url[] = "https://github.com/leonard-peng/management_of_classes";

const int sub_menu_number[MENU_NUM] = {4, 3, 3, 5, 2};

const char * const buttons[] = {"取消", "确定"};

const char * const win_titles_string[] = {"教学楼信息一览表", "教室信息一览表", "排课信息一览表"};

const char * const count_win_titles_string[] = {"教学楼设施详情", "教学楼开课详情", "教学楼上课时间", "学院开课详情", "课程开设详情"};

const char * const building_string[] = {"教学楼编号", "教学楼名称", "管理员电话", "教室的间数", "容纳学生数", "教室总面积"};

const char * const classroom_string[] = {"教学楼编号", "教室的编号", "所在的楼层", "教室的面积", "容纳学生数", "多媒体教室"};

const char * const arrangement_string[] = {"教学楼编号", "教室的编号", "课程的学期", "课程的周次", "课程的星期", "课程的节次", "课程的名字", "学生的人数", "所属的学院"};

const char * const count_1_string[] = {"教学楼名字", "教室间数", "容纳学生数", "教室总面积", "多媒体教室间数"};

const char * const count_2_string[] = {"教学楼名字", "开课门数", "开课总学时数", "听课学生人次数", "使用单位数"};

const char * const count_3_string[] = {" 教室 ", "  1 - 2 节  ", "  3 - 4 节  ", "  5 - 6 节  ", "  7 - 8 节  ", "  9 - 10 节  ", "  11 - 12 节  "};

const char * const count_4_string[] = {"课程名", "教学楼", "教室编号", "周次", "星期", "节次"};

const char * const count_5_string[] = {"教学楼", "教室编号", "学院", "周次", "星期", "节次"};

const char * const pop_up_menu_string[] = {"显示详情", "修改数据", "删除数据"};

const char * const menu_string[] =
{
    "文件", "新建", "打开", "保存", "另存为",
    "信息录入", "新教学楼", "新教室", "新课程",
    "信息查询", "查询教学楼", "查询教室", "查询课程",
    "信息统计", "统计各教学楼设施情况", "统计各教学楼开课情况", "统计某教学楼上课时间", "统计某学院开课情况", "统计某课程开设情况",
    "帮助", "关于", "帮助",
};

//  对应 menubar 中的选项
void (*handle[])() = {  file_new, file_open, file_save, file_save_as,
                        new_building, new_classroom, new_arrangement,
                        search_building, search_classroom, search_arrangement,
                        count_1, count_2, count_3, count_4, count_5,
                        help_about, help  };




/**************************
**		图形界面函数       **
**************************/

//  创建主界面
void create_main(void)
{
    //  创建 main window
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win), 0);
    gtk_window_set_title(GTK_WINDOW(win), name);
    gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win);
    g_signal_connect(win, "destroy", gtk_main_quit, NULL);

    //  创建 vbox
    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win), vbox);

    //  创建 menubar
    GtkWidget *menubar = gtk_menu_bar_new();
    create_menubar(win, menubar);
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);

    //  创建 label
    GtkWidget *label = gtk_label_new(win_titles_string[0]);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 8);

    //  创建  treeview(数据显示界面)
    list_building = gtk_list_store_new(7, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_FLOAT);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_building));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_number = gtk_tree_view_column_new_with_attributes(building_string[0], render, "text", NUMBER, NULL);
    GtkTreeViewColumn *col_name = gtk_tree_view_column_new_with_attributes(building_string[1], render, "text", NAME, NULL);
    GtkTreeViewColumn *col_phone = gtk_tree_view_column_new_with_attributes(building_string[2], render, "text", PHONE, NULL);
    GtkTreeViewColumn *col_classes = gtk_tree_view_column_new_with_attributes(building_string[3], render, "text", CLASSES, NULL);
    GtkTreeViewColumn *col_students = gtk_tree_view_column_new_with_attributes(building_string[4], render, "text", STUDENTS, NULL);
    GtkTreeViewColumn *col_square = gtk_tree_view_column_new_with_attributes(building_string[5], render, "text", SQUARE, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_number);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_name);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_phone);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_classes);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_students);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_square);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);
    gtk_widget_show_all(win);

    //  创建右键菜单
    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
    GtkWidget *Pop_up_menu_building = gtk_menu_new();
    GtkWidget *menu_item;

    menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[0]);
    g_signal_connect(menu_item, "activate", G_CALLBACK(menu_show_classroom), NULL);
    gtk_widget_show(menu_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu_building), menu_item);

    menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[1]);
    g_signal_connect(menu_item, "activate", G_CALLBACK(menu_change_building_main), NULL);
    gtk_widget_show(menu_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu_building), menu_item);

    menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[2]);
    g_signal_connect(menu_item, "activate", G_CALLBACK(menu_delete_building_main), NULL);
    gtk_widget_show(menu_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu_building), menu_item);

    struct Pop_up_menu_building *menu = (struct Pop_up_menu_building *)malloc(sizeof(struct Pop_up_menu_building));
    menu->menu = Pop_up_menu_building;
    menu->treeview = GTK_TREE_VIEW(treeview);
    menu->selection = selection;
    menu->active = &menu_building;

    g_signal_connect_swapped(treeview, "button_press_event", G_CALLBACK(handle_Pop_up_menu_building), menu);
}

//  创建  menubar(左上角下拉菜单)
void create_menubar(GtkWidget *win, GtkWidget *menubar)
{
    int i, j, k = 0;
    char **strings = (char **)menu_string;
    for (i = 0; i < MENU_NUM; i++)
    {
        GtkWidget *menu_item = gtk_menu_item_new_with_label(*strings++);
        gtk_menu_shell_append((GtkMenuShell *)menubar, menu_item);
        GtkWidget *submenu = gtk_menu_new();
        gtk_menu_item_set_submenu((GtkMenuItem *)menu_item, submenu);
        for (j = 0; j  < sub_menu_number[i]; j++, k++)
        {
            GtkWidget *submenu_item = gtk_menu_item_new_with_label(*strings++);
            g_signal_connect(submenu_item, "activate", G_CALLBACK(*(handle+k)), (gpointer)(win));
            gtk_menu_shell_append((GtkMenuShell *)submenu, submenu_item);
        }
    }

}

//  创建选择文件的界面
char *choose_file(int file_operation)
{
    GtkWidget *file_chooser;
    if (file_operation == OPEN_FILE)
    {
        file_chooser = gtk_file_chooser_dialog_new("打开", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, ("取消"), GTK_RESPONSE_CANCEL, ("确定"),  GTK_RESPONSE_ACCEPT, NULL);
    }
    else if (file_operation == SAVE_FILE)
    {
        file_chooser = gtk_file_chooser_dialog_new("新建", NULL, GTK_FILE_CHOOSER_ACTION_SAVE, ("取消"), GTK_RESPONSE_CANCEL, ("确定"),  GTK_RESPONSE_ACCEPT, NULL);
        gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(file_chooser), TRUE);
    }
    else if (file_operation == SAVE_FILE_AS)
    {
        file_chooser = gtk_file_chooser_dialog_new("另存为", NULL, GTK_FILE_CHOOSER_ACTION_SAVE, ("取消"), GTK_RESPONSE_CANCEL, ("确定"),  GTK_RESPONSE_ACCEPT, NULL);
        gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(file_chooser), TRUE);
    }

    //  文件类型选择(仅支持后缀为 .con)
    GtkFileFilter *filter = gtk_file_filter_new();
    gtk_file_filter_add_pattern(filter, "*.con");
    gtk_file_filter_set_name(filter, "数据存储文件（*.con）");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), filter);

    if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
    {
        char *temp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
        char *file_name = malloc(300);
        strcpy(file_name, temp);
        g_free(temp);
        if(file_operation == SAVE_FILE || file_operation == SAVE_FILE_AS)
        {
            //  给文件加上后缀名 .con
            int len = strlen(file_name);
            if (file_name[len-4] != '.' || file_name[len-3] != 'c' || file_name[len-2] != 'o' || file_name[len-1] != 'n')
            {
                strcat(file_name, ".con");
            }
        }
        gtk_widget_destroy(file_chooser);
        return file_name;
    }
    else
    {
        gtk_widget_destroy(file_chooser);
        return NULL;
    }
}

//  创建教学楼信息编辑界面
void edit_building(int data_condition)
{
    GtkWidget *building_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_transient_for(GTK_WINDOW(building_win), GTK_WINDOW(win));
    gtk_window_set_modal(GTK_WINDOW(building_win), TRUE);
    gtk_window_set_destroy_with_parent(GTK_WINDOW(building_win), TRUE);
    gtk_window_set_position(GTK_WINDOW(building_win), GTK_WIN_POS_CENTER);

    if (data_condition == NEW)
    {
        gtk_window_set_title(GTK_WINDOW(building_win), "添加新教学楼");
    }
    else if (data_condition == EDITED)
    {
        gtk_window_set_title(GTK_WINDOW(building_win), "修改教学楼信息");
    }

    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_container_add(GTK_CONTAINER(building_win), box);

    GtkWidget *box_number  = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_number = gtk_label_new(building_string[0]);
    GtkWidget *entry_number = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_number), label_number, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_number), entry_number, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_number, FALSE, TRUE, 4);

    GtkWidget *box_name = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_name = gtk_label_new(building_string[1]);
    GtkWidget *entry_name = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_name), label_name, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_name), entry_name, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_name, FALSE, TRUE, 4);

    GtkWidget *box_phone  = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_phone = gtk_label_new(building_string[2]);
    GtkWidget *entry_phone = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_phone), label_phone, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_phone), entry_phone, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_phone, FALSE, TRUE, 4);

    GtkWidget *box_classes = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_classes = gtk_label_new(building_string[3]);
    GtkWidget *entry_classes = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_classes), label_classes, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_classes), entry_classes, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_classes, FALSE, TRUE, 4);

    GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
    GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box), box_buttons, FALSE, TRUE, 4);

    struct Building_Entry *data = (struct Building_Entry *)malloc(sizeof(struct Building_Entry));
    data->entry_number = entry_number;
    data->entry_name = entry_name;
    data->entry_phone = entry_phone;
    data->entry_classes = entry_classes;

    if (data_condition == NEW)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_new_building), (gpointer)data);
    }
    else if (data_condition == EDITED)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_change_building), (gpointer)data);
    }
    g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), (gpointer)building_win);
    g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), (gpointer)building_win);

    gtk_widget_show_all(building_win);
}


//  创建教室信息编辑界面
void edit_classroom(int data_condition)
{
    GtkWidget *classroom_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_transient_for(GTK_WINDOW(classroom_win), GTK_WINDOW(win));
    gtk_window_set_modal(GTK_WINDOW(classroom_win), TRUE);
    gtk_window_set_destroy_with_parent(GTK_WINDOW(classroom_win), TRUE);
    gtk_window_set_position(GTK_WINDOW(classroom_win), GTK_WIN_POS_CENTER);

    if (data_condition == NEW)
    {
        gtk_window_set_title(GTK_WINDOW(classroom_win), "添加新教室");
    }
    else if (data_condition == EDITED)
    {
        gtk_window_set_title(GTK_WINDOW(classroom_win), "修改教室信息");
    }
    else if (data_condition == SEARCH)
    {
        gtk_window_set_title(GTK_WINDOW(classroom_win), "查询相关教室");
    }

    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 7);
    gtk_container_add(GTK_CONTAINER(classroom_win), box);


    GtkWidget *box_building_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_building_number = gtk_label_new(classroom_string[0]);
    GtkWidget *combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(list_building));
    gtk_combo_box_set_id_column(GTK_COMBO_BOX(combo), NUMBER);
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    if (data_condition != EDITED)
    {
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), render, TRUE);
        gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), render, "text", NUMBER, NULL);
        gtk_box_pack_start(GTK_BOX(box_building_number), label_building_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_building_number), combo, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box), box_building_number, FALSE, TRUE, 4);
    }

    GtkWidget *box_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_number = gtk_label_new(classroom_string[1]);
    GtkWidget *entry_number = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_number), label_number, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_number), entry_number, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_number, FALSE, TRUE, 4);

    GtkWidget *box_floor = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_floor = gtk_label_new(classroom_string[2]);
    GtkWidget *entry_floor = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_floor), label_floor, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_floor), entry_floor, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_floor, FALSE, TRUE, 4);

    GtkWidget *box_square = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_square = gtk_label_new(classroom_string[3]);
    GtkWidget *entry_square = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_square), label_square, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_square), entry_square, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_square, FALSE, TRUE, 4);

    GtkWidget *box_sudents = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_sudents = gtk_label_new(classroom_string[4]);
    GtkWidget *entry_sudents = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_sudents), label_sudents, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_sudents), entry_sudents, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_sudents, FALSE, TRUE, 4);

    GtkWidget *box_choices = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 3);
    GtkWidget *label_choices = gtk_label_new(classroom_string[5]);
    GtkWidget *choice_yes = gtk_radio_button_new_with_label(NULL, "是");
    GtkWidget *choice_no = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(choice_yes), "否");
    gtk_box_pack_start(GTK_BOX(box_choices), label_choices, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_choices), choice_yes, TRUE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_choices), choice_no, TRUE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_choices, FALSE, TRUE, 4);

    GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
    GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box), box_buttons, FALSE, TRUE, 4);

    struct Classroom_Entry *data = (struct Classroom_Entry *)malloc(sizeof(struct Classroom_Entry));
    data->combo = combo;
    data->entry_number = entry_number;
    data->entry_floor = entry_floor;
    data->entry_square = entry_square;
    data->entry_students = entry_sudents;
    data->choice = choice_yes;

    if (data_condition == NEW)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_new_classroom), (gpointer)data);
    }
    else if (data_condition == EDITED)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_change_classroon), (gpointer)data);
    }
    else if (data_condition == SEARCH)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_search_classroom_data), (gpointer)data);
    }
    g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), (gpointer)classroom_win);
    g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), (gpointer)classroom_win);


    gtk_widget_show_all(classroom_win);
}

//  创建编辑排课信息界面
void edit_arrangement(int data_condition)
{
    GtkWidget *arrangement_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_transient_for(GTK_WINDOW(arrangement_win), GTK_WINDOW(win));
    gtk_window_set_modal(GTK_WINDOW(arrangement_win), TRUE);
    gtk_window_set_destroy_with_parent(GTK_WINDOW(arrangement_win), TRUE);
    gtk_window_set_position(GTK_WINDOW(arrangement_win), GTK_WIN_POS_CENTER);

    if (data_condition == NEW)
    {
        gtk_window_set_title(GTK_WINDOW(arrangement_win), "添加新课程");
    }
    else if (data_condition == EDITED)
    {
        gtk_window_set_title(GTK_WINDOW(arrangement_win), "修改课程信息");
    }
    else if (data_condition == SEARCH)
    {
        gtk_window_set_title(GTK_WINDOW(arrangement_win), "查找相关课程");
    }

    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 9);
    gtk_container_add(GTK_CONTAINER(arrangement_win), box);


    GtkWidget *entry_building_number = NULL;
    GtkWidget *combo = NULL;
    if (data_condition == SEARCH)
    {
        GtkWidget *box_building_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *label_building_number = gtk_label_new(arrangement_string[0]);
        entry_building_number = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_building_number), label_building_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_building_number), entry_building_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box), box_building_number, FALSE, TRUE, 4);
    }
    else if (data_condition == NEW)
    {
        GtkWidget *box_building_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *label_building_number = gtk_label_new(arrangement_string[0]);
        combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(list_building));
        gtk_combo_box_set_id_column(GTK_COMBO_BOX(combo), NUMBER);
        gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
        GtkCellRenderer *render = gtk_cell_renderer_text_new();
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), render, TRUE);
        gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), render, "text", NUMBER, NULL);
        gtk_box_pack_start(GTK_BOX(box_building_number), label_building_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_building_number), combo, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box), box_building_number, FALSE, TRUE, 4);
    }

    GtkWidget *entry_classroom_number;
    if (data_condition != EDITED)
    {
        GtkWidget *box_classroom_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *label_classroom_number = gtk_label_new(arrangement_string[1]);
        entry_classroom_number = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_classroom_number), label_classroom_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_classroom_number), entry_classroom_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box), box_classroom_number, FALSE, TRUE, 4);
    }

    GtkWidget *box_term = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_term = gtk_label_new(arrangement_string[2]);
    GtkWidget *entry_term = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_term), label_term, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_term), entry_term, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_term, FALSE, TRUE, 4);

    GtkWidget *box_weeks_of_term = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_weeks_of_term = gtk_label_new(arrangement_string[3]);
    GtkWidget *entry_weeks_of_term = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_weeks_of_term), label_weeks_of_term, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_weeks_of_term), entry_weeks_of_term, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_weeks_of_term, FALSE, TRUE, 4);

    GtkWidget *box_weekday = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_weekday = gtk_label_new(arrangement_string[4]);
    GtkWidget *entry_weekday = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_weekday), label_weekday, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_weekday), entry_weekday, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_weekday, FALSE, TRUE, 4);

    GtkWidget *box_time_of_day = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_time_of_day = gtk_label_new(arrangement_string[5]);
    GtkWidget *entry_time_of_day = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_time_of_day), label_time_of_day, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_time_of_day), entry_time_of_day, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_time_of_day, FALSE, TRUE, 4);

    GtkWidget *box_name = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_name = gtk_label_new(arrangement_string[6]);
    GtkWidget *entry_name = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_name), label_name, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_name), entry_name, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_name, FALSE, TRUE, 4);

    GtkWidget *entry_students = NULL;
    if (data_condition != SEARCH)
    {
        GtkWidget *box_students = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *label_students = gtk_label_new(arrangement_string[7]);
        entry_students = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_students), label_students, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_students), entry_students, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box), box_students, FALSE, TRUE, 4);
    }

    GtkWidget *box_colledge = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_colledge = gtk_label_new(arrangement_string[8]);
    GtkWidget *entry_colledge = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_colledge), label_colledge, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_colledge), entry_colledge, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_colledge, FALSE, TRUE, 4);

    GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
    GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box), box_buttons, FALSE, TRUE, 4);

    struct Arrangement_Entry *data = (struct Arrangement_Entry *)malloc(sizeof(struct Arrangement_Entry));
    if (data_condition == SEARCH)
    {
        data->entry_building_number = entry_building_number;
    }
    else
    {
        data->combo = combo;
    }
    data->entry_classroom_number = entry_classroom_number;
    data->entry_term = entry_term;
    data->entry_weeks_of_term = entry_weeks_of_term;
    data->entry_weekday = entry_weekday;
    data->entry_time_of_day = entry_time_of_day;
    data->entry_name = entry_name;
    if (data_condition != SEARCH)
    {
        data->entry_students = entry_students;
    }
    data->entry_colledge = entry_colledge;

    if (data_condition == NEW)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_new_arrangement), (gpointer)data);
    }
    else if (data_condition == EDITED)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_change_arrangement), (gpointer)data);
    }
    else if (data_condition == SEARCH)
    {
        g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_search_arrangement_data), (gpointer)data);
    }
    g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), (gpointer)arrangement_win);
    g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), (gpointer)arrangement_win);

    gtk_widget_show_all(arrangement_win);
}

//  创建搜寻相关教学楼界面
void search_building(void)
{
    GtkWidget *search_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_transient_for(GTK_WINDOW(search_win), GTK_WINDOW(win));
    gtk_window_set_modal(GTK_WINDOW(search_win), TRUE);
    gtk_window_set_destroy_with_parent(GTK_WINDOW(search_win), TRUE);
    gtk_window_set_position(GTK_WINDOW(search_win), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(search_win), "查询教学楼");

    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
    gtk_container_add(GTK_CONTAINER(search_win), box);

    GtkWidget *box_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_number = gtk_label_new(building_string[0]);
    GtkWidget *entry_number = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_number), label_number, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_number), entry_number, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_number, FALSE, TRUE, 4);

    GtkWidget *box_name = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *label_name = gtk_label_new(building_string[1]);
    GtkWidget *entry_name = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_name), label_name, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box_name), entry_name, FALSE, TRUE, 8);
    gtk_box_pack_start(GTK_BOX(box), box_name, FALSE, TRUE, 4);

    GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
    GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(box), box_buttons, FALSE, TRUE, 4);

    struct Building_Entry *data = (struct Building_Entry *)malloc(sizeof(struct Building_Entry));
    data->entry_number = entry_number;
    data->entry_name = entry_name;

    g_signal_connect(button_accept, "clicked", G_CALLBACK(handle_search_building_data), (gpointer)data);
    g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), (gpointer)search_win);
    g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), (gpointer)search_win);

    gtk_widget_show_all(search_win);
}

//  搜寻相关教室
void search_classroom(void)
{
    edit_classroom(SEARCH);
}

//  搜寻相关课程
void search_arrangement(void)
{
    edit_arrangement(SEARCH);
}

//  录入新的教学楼信息
void new_building(void)
{
    if (fp != NULL)
    {
        edit_building(NEW);
    }
    else
    {
        errmesg("请先打开或新建数据文件！");
    }
}


//  录入新的教室信息
void new_classroom(void)
{
    if (fp != NULL)
    {
        if (head != NULL)
        {
            edit_classroom(NEW);
        }
        else
        {
            errmesg("请先录入教学楼信息！");
        }
    }
    else
    {
        errmesg("请先打开或新建数据文件");
    }
}

//  录入新的课程信息
void new_arrangement(void)
{
    if (fp != NULL)
    {
        if (head != NULL)
        {
            Building* temp = head;
            while (temp->classroom == NULL && temp != NULL)
            {
                temp = temp->next;
            }

            if (temp == NULL)
            {
                errmesg("请先录入教室信息");
            }
            else
            {
                edit_arrangement(NEW);
            }
        }
        else
        {
            errmesg("请先录入教学楼信息！");
        }
    }
    else
    {
        errmesg("请先打开或新建数据文件");
    }
}

//  创建统计功能一界面
void count_1(void)
{
    if (head == NULL)
    {
        errmesg("请先录入相关信息！");
    }
    else
    {
        GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
        gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[0]);
        gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
        gtk_widget_realize(win_count);
        g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

        GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add(GTK_CONTAINER(win_count), vbox);

        GtkListStore *list;
        list = gtk_list_store_new(6, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_FLOAT, G_TYPE_INT);
        GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list));
        GtkCellRenderer *render = gtk_cell_renderer_text_new();
        g_object_set(G_OBJECT(render), "foreground", "black", NULL);

        GtkTreeViewColumn *col_1 = gtk_tree_view_column_new_with_attributes(count_1_string[0], render, "text", 1, NULL);
        GtkTreeViewColumn *col_2 = gtk_tree_view_column_new_with_attributes(count_1_string[1], render, "text", 2, NULL);
        GtkTreeViewColumn *col_3 = gtk_tree_view_column_new_with_attributes(count_1_string[2], render, "text", 3, NULL);
        GtkTreeViewColumn *col_4 = gtk_tree_view_column_new_with_attributes(count_1_string[3], render, "text", 4, NULL);
        GtkTreeViewColumn *col_5 = gtk_tree_view_column_new_with_attributes(count_1_string[4], render, "text", 5, NULL);

        gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_1);
        gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_2);
        gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_3);
        gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_4);
        gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_5);

        gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);

        GtkWidget *button = gtk_button_new_with_label("关闭");
        g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_count);
        gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

        //  统计各教学楼基本情况
        int classrooms;
        int media_classrooms;
        Building *current_building = head;
        while (current_building != NULL)
        {
            classrooms = 0;
            media_classrooms = 0;
            Classroom *current_classroom = current_building->classroom;
            while (current_classroom != NULL)
            {
                classrooms += 1;
                if (current_classroom->has_media == '1')
                {
                    media_classrooms += 1;
                }
                current_classroom = current_classroom->next;
            }
            gtk_list_store_insert_with_values(list, &treeiter, 0,
                                                0, (gpointer)current_building,
                                                1, current_building->name,
                                                2, classrooms,
                                                3, current_building->students,
                                                4, current_building->square,
                                                5, media_classrooms,
                                                -1);
            current_building = current_building->next;
        }

        gtk_widget_show_all(win_count);
    }
}

//  创建统计功能二界面
void count_2(void)
{
    if (head == NULL)
    {
        errmesg("请先录入相关信息！");
    }
    else
    {
        GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
        gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[1]);
        gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
        gtk_widget_realize(win_count);
        g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

        GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add(GTK_CONTAINER(win_count), vbox);

        GtkWidget *box_term = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_term = gtk_label_new("学期");
        GtkWidget *entry_term = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_term), label_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_term), entry_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_term, FALSE, TRUE, 4);

        GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
        GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(vbox), box_buttons, FALSE, TRUE, 4);

        g_signal_connect(button_accept, "clicked", G_CALLBACK(count_2_handle_data), entry_term);
        g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), win_count);
        g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), win_count);

        gtk_widget_show_all(win_count);

    }
}

//  创建统计功能三界面
void count_3(void)
{
    if (head == NULL)
    {
        errmesg("请先录入相关数据！");
    }
    else
    {
        GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
        gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[2]);
        gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
        gtk_widget_realize(win_count);
        g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

        GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add(GTK_CONTAINER(win_count), vbox);

        GtkWidget *box_term = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_term = gtk_label_new("    学        期   ");
        GtkWidget *entry_term = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_term), label_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_term), entry_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_term, FALSE, TRUE, 4);

        GtkWidget *box_week = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_week = gtk_label_new("    周        次   ");
        GtkWidget *entry_week = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_week), label_week, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_week), entry_week, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_week, FALSE, TRUE, 4);

        GtkWidget *box_weekday = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_weekday = gtk_label_new("    星        期   ");
        GtkWidget *entry_weekday = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_weekday), label_weekday, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_weekday), entry_weekday, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_weekday, FALSE, TRUE, 4);

        GtkWidget *box_number = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_number = gtk_label_new("教学楼编号");
        GtkWidget *entry_number = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_number), label_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_number), entry_number, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_number, FALSE, TRUE, 4);

        GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
        GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(vbox), box_buttons, FALSE, TRUE, 4);

        struct Count_3_ENTRY *data = (struct Count_3_ENTRY *)malloc(sizeof(struct Count_3_ENTRY));
        data->entry_term = entry_term;
        data->entry_week = entry_week;
        data->entry_weekday = entry_weekday;
        data->entry_number = entry_number;

        g_signal_connect(button_accept, "clicked", G_CALLBACK(count_3_handle_data), data);
        g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), win_count);
        g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), win_count);

        gtk_widget_show_all(win_count);
    }
}

//  创建统计功能四界面
void count_4(void)
{
    if (head == NULL)
    {
        errmesg("请先录入相关数据！");
    }
    else
    {
        GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
        gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[3]);
        gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
        gtk_widget_realize(win_count);
        g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

        GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add(GTK_CONTAINER(win_count), vbox);

        GtkWidget *box_term = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_term = gtk_label_new("学期");
        GtkWidget *entry_term = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_term), label_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_term), entry_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_term, FALSE, TRUE, 4);

        GtkWidget *box_colledge = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_colledge = gtk_label_new("学院");
        GtkWidget *entry_colledge = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_colledge), label_colledge, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_colledge), entry_colledge, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_colledge, FALSE, TRUE, 4);

        GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
        GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(vbox), box_buttons, FALSE, TRUE, 4);

        struct Count_4_ENTRY *data = (struct Count_4_ENTRY *)malloc(sizeof(struct Count_4_ENTRY));
        data->entry_term = entry_term;
        data->entry_colledge = entry_colledge;

        g_signal_connect(button_accept, "clicked", G_CALLBACK(count_4_handle_data), data);
        g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), win_count);
        g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), win_count);

        gtk_widget_show_all(win_count);
    }
}

//  创建统计功能五界面
void count_5(void)
{
    if (head == NULL)
    {
        errmesg("请先录入相关信息！");
    }
    else
    {
        GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
        gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[4]);
        gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
        gtk_widget_realize(win_count);
        g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

        GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add(GTK_CONTAINER(win_count), vbox);

        GtkWidget *box_term = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_term = gtk_label_new("学期");
        GtkWidget *entry_term = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_term), label_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_term), entry_term, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_term, FALSE, TRUE, 4);

        GtkWidget *box_arrangement = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        GtkWidget *label_arrangement = gtk_label_new("课程");
        GtkWidget *entry_arrangement = gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(box_arrangement), label_arrangement, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(box_arrangement), entry_arrangement, FALSE, TRUE, 8);
        gtk_box_pack_start(GTK_BOX(vbox), box_arrangement, FALSE, TRUE, 4);

        GtkWidget *box_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
        GtkWidget *button_cancle = gtk_button_new_with_label(buttons[0]);
        GtkWidget *button_accept = gtk_button_new_with_label(buttons[1]);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_cancle, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(box_buttons), button_accept, TRUE, TRUE, 10);
        gtk_box_pack_start(GTK_BOX(vbox), box_buttons, FALSE, TRUE, 4);

        struct Count_5_ENTRY *data = (struct Count_5_ENTRY *)malloc(sizeof(struct Count_5_ENTRY));
        data->entry_arrangement = entry_arrangement;
        data->entry_term = entry_term;

        g_signal_connect(button_accept, "clicked", G_CALLBACK(count_5_handle_data), data);
        g_signal_connect_after(button_accept, "clicked", G_CALLBACK(destroy_window), win_count);
        g_signal_connect(button_cancle, "clicked", G_CALLBACK(destroy_window), win_count);

        gtk_widget_show_all(win_count);

    }
}

//  创建显示教学楼数据详情界面
void show_building_detail(void)
{
    if (list_building_detail != NULL)
    {
        gtk_list_store_clear(list_building_detail);
    }

    win_building = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_building), 0);
    gtk_window_set_title(GTK_WINDOW(win_building), win_titles_string[0]);
    gtk_window_set_position(GTK_WINDOW(win_building), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_building);
    g_signal_connect(win_building, "destroy", G_CALLBACK(destroy_window), (gpointer)win_building);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_building), vbox);

    list_building_detail = gtk_list_store_new(7, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_FLOAT);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_building_detail));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_number = gtk_tree_view_column_new_with_attributes(building_string[0], render, "text", NUMBER, NULL);
    GtkTreeViewColumn *col_name = gtk_tree_view_column_new_with_attributes(building_string[1], render, "text", NAME, NULL);
    GtkTreeViewColumn *col_phone = gtk_tree_view_column_new_with_attributes(building_string[2], render, "text", PHONE, NULL);
    GtkTreeViewColumn *col_classes = gtk_tree_view_column_new_with_attributes(building_string[3], render, "text", CLASSES, NULL);
    GtkTreeViewColumn *col_students = gtk_tree_view_column_new_with_attributes(building_string[4], render, "text", STUDENTS, NULL);
    GtkTreeViewColumn *col_square = gtk_tree_view_column_new_with_attributes(building_string[5], render, "text", SQUARE, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_number);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_name);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_phone);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_classes);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_students);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_square);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);


    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_building);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);
}

//  创建显示教室数据详情界面
void show_classroom_detail(int flag)
{
    if(list_classroom_detail != NULL)
    {
        gtk_list_store_clear(list_classroom_detail);
    }

    win_classroom = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_classroom), 0);
    gtk_window_set_title(GTK_WINDOW(win_classroom), win_titles_string[1]);
    gtk_window_set_position(GTK_WINDOW(win_classroom), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_classroom);
    g_signal_connect(win_classroom, "destroy", G_CALLBACK(destroy_window), (gpointer)win_classroom);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_classroom), vbox);

    list_classroom_detail = gtk_list_store_new(7, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_FLOAT, G_TYPE_INT, G_TYPE_STRING);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_classroom_detail));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_building_number = gtk_tree_view_column_new_with_attributes(classroom_string[0], render, "text", NUMBER_BUILDING, NULL);
    GtkTreeViewColumn *col_number = gtk_tree_view_column_new_with_attributes(classroom_string[1], render, "text", NUMBER_CLASSROOM, NULL);
    GtkTreeViewColumn *col_floor = gtk_tree_view_column_new_with_attributes(classroom_string[2], render, "text", FLOOR, NULL);
    GtkTreeViewColumn *col_square = gtk_tree_view_column_new_with_attributes(classroom_string[3], render, "text", SQUARE_CLASSROOM, NULL);
    GtkTreeViewColumn *col_students = gtk_tree_view_column_new_with_attributes(classroom_string[4], render, "text", STUDENTS_CLASSROOM, NULL);
    GtkTreeViewColumn *col_media = gtk_tree_view_column_new_with_attributes(classroom_string[5], render, "text", MEDIA, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_building_number);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_number);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_floor);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_square);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_students);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_media);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);

    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_classroom);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

    if (flag == 1)
    {
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
        GtkWidget *Pop_up_menu = gtk_menu_new();
        GtkWidget *menu_item;

        menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[0]);
        g_signal_connect(menu_item, "activate", G_CALLBACK(menu_show_arrangement), NULL);
        gtk_widget_show(menu_item);
        gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu), menu_item);

        menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[1]);
        g_signal_connect(menu_item, "activate", G_CALLBACK(menu_change_classroom), NULL);
        gtk_widget_show(menu_item);
        gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu), menu_item);

        menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[2]);
        g_signal_connect(menu_item, "activate", G_CALLBACK(menu_delete_classroom), NULL);
        gtk_widget_show(menu_item);
        gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu), menu_item);

        struct Pop_up_menu_classroom *menu = (struct Pop_up_menu_classroom *)malloc(sizeof(struct Pop_up_menu_classroom));
        menu->menu = Pop_up_menu;
        menu->treeview = GTK_TREE_VIEW(treeview);
        menu->selection = selection;
        menu->active = &menu_classroom;

        g_signal_connect_swapped(treeview, "button_press_event", G_CALLBACK(handle_Pop_up_menu_classroom), menu);
    }
}

//  创建显示排课数据详情界面
void show_arrangement_detail(int flag)
{
    if (list_arrangement_detail != NULL)
    {
        gtk_list_store_clear(list_arrangement_detail);
    }

    win_arrangement = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_arrangement), 0);
    gtk_window_set_title(GTK_WINDOW(win_arrangement), win_titles_string[2]);
    gtk_window_set_position(GTK_WINDOW(win_arrangement), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_arrangement);
    g_signal_connect(win_arrangement, "destroy", G_CALLBACK(destroy_window), (gpointer)win_arrangement);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_arrangement), vbox);

    list_arrangement_detail = gtk_list_store_new(10, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_arrangement_detail));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_number_building = gtk_tree_view_column_new_with_attributes(arrangement_string[0], render, "text", NUMBER_BUILDING_ARRANGEMENT, NULL);
    GtkTreeViewColumn *col_number_classroom = gtk_tree_view_column_new_with_attributes(arrangement_string[1], render, "text", NUMBER_CLASSROOM_ARRANGEMENT, NULL);
    GtkTreeViewColumn *col_term = gtk_tree_view_column_new_with_attributes(arrangement_string[2], render, "text", TERM, NULL);
    GtkTreeViewColumn *col_week_of_term = gtk_tree_view_column_new_with_attributes(arrangement_string[3], render, "text", WEEK_OF_TERM, NULL);
    GtkTreeViewColumn *col_weekday = gtk_tree_view_column_new_with_attributes(arrangement_string[4], render, "text", WEEKDAY, NULL);
    GtkTreeViewColumn *col_time_of_day = gtk_tree_view_column_new_with_attributes(arrangement_string[5], render, "text", TIME_OF_DAY, NULL);
    GtkTreeViewColumn *col_name = gtk_tree_view_column_new_with_attributes(arrangement_string[6], render, "text", NAME_ARRANGEMENT, NULL);
    GtkTreeViewColumn *col_students = gtk_tree_view_column_new_with_attributes(arrangement_string[7], render, "text", STUDENTS_ARRANGEMENT, NULL);
    GtkTreeViewColumn *col_colledge = gtk_tree_view_column_new_with_attributes(arrangement_string[8], render, "text", COLLEDGE, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_number_building);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_number_classroom);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_term);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_week_of_term);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_weekday);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_time_of_day);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_name);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_students);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_colledge);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);


    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_arrangement);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

    if (flag == 1)
    {
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
        GtkWidget *Pop_up_menu = gtk_menu_new();
        GtkWidget *menu_item;

        menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[1]);
        g_signal_connect(menu_item, "activate", G_CALLBACK(menu_change_arrangement), NULL);
        gtk_widget_show(menu_item);
        gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu), menu_item);

        menu_item = gtk_menu_item_new_with_label(pop_up_menu_string[2]);
        g_signal_connect(menu_item, "activate", G_CALLBACK(menu_delete_arrangement), NULL);
        gtk_widget_show(menu_item);
        gtk_menu_shell_append(GTK_MENU_SHELL(Pop_up_menu), menu_item);

        struct Pop_up_menu_arrangement *menu = (struct Pop_up_menu_arrangement *)malloc(sizeof(struct Pop_up_menu_arrangement));
        menu->menu = Pop_up_menu;
        menu->treeview = GTK_TREE_VIEW(treeview);
        menu->selection = selection;
        menu->active = &menu_arrangement;

        g_signal_connect_swapped(treeview, "button_press_event", G_CALLBACK(handle_Pop_up_menu_classroom), menu);
    }
}

// 右键菜单显示教室信息
void menu_show_classroom(GtkWidget *ignored)
{
    Classroom *classroom = menu_building->classroom;
    if(classroom == NULL)
    {
        errmesg("尚未录入教室信息！");
    }
    else
    {
        show_classroom_detail(1);
        while(classroom != NULL)
        {
            list_classroom_insert(list_classroom_detail, menu_building->number, classroom);
            classroom = classroom->next;
        }
        gtk_widget_show_all(win_classroom);
    }
}

// 右键菜单显示排课信息
void menu_show_arrangement (GtkWidget *ignored)
{
    Arrangement *arrangement = menu_classroom->arrangement;
    if(arrangement == NULL)
    {
        errmesg("尚未录入排课信息！");
    }
    else
    {
        show_arrangement_detail(1);
        while(arrangement != NULL)
        {
            list_arrangement_insert(list_arrangement_detail, menu_building->number, menu_classroom->number, arrangement);
            arrangement = arrangement->next;
        }
        gtk_widget_show_all(win_arrangement);
    }
}

//  右键菜单修改教学楼信息
void menu_change_building_main(GtkWidget *ignored)
{
    edit_building(EDITED);
}

//  右键菜单修改教室信息
void menu_change_classroom(GtkWidget *ignored)
{
    edit_classroom(EDITED);
}

//  右键菜单修改排课信息
void menu_change_arrangement(GtkWidget *ignored)
{
    edit_arrangement(EDITED);
}

//  右键菜单删除教学楼信息
void menu_delete_building_main(GtkWidget *ignored)
{
    if (menu_building == head)
    {
        head = menu_building->next;
    }
    else
    {
        Building *temp = head;
        while (temp->next != menu_building)
        {
            temp = temp->next;
        }
        temp->next = menu_building->next;
    }

    Classroom *classroom = menu_building->classroom;
    while(classroom != NULL)
    {
        Arrangement *arrangement = classroom->arrangement;
        while(arrangement != NULL)
        {
            Arrangement *temp = arrangement;
            arrangement = arrangement->next;
            free(temp);
        }
        Classroom *temp = classroom;
        classroom = classroom->next;
        free(temp);
    }
    free(menu_building);

    gtk_list_store_remove(list_building, &treeiter);
}

//  右键菜单删除教室信息
void menu_delete_classroom(GtkWidget *ignored)
{
    if (menu_building->classroom == menu_classroom)
    {
        menu_building->classroom = menu_classroom->next;
        free(menu_classroom);
    }
    else
    {
        Classroom *previous = menu_building->classroom;
        while (previous->next != menu_classroom)
        {
            previous = previous->next;
        }
        previous->next = menu_classroom->next;
        free(menu_classroom);
    }
    gtk_list_store_remove(list_classroom_detail, &treeiter);
}

//  右键菜单删除排课信息
void menu_delete_arrangement(GtkWidget *ignored)
{

    if (menu_classroom->arrangement == menu_arrangement)
    {
        menu_classroom->arrangement = menu_arrangement->next;
        free(menu_arrangement);
    }
    else
    {
        Arrangement *previous = menu_classroom->arrangement;
        while (previous->next != menu_arrangement)
        {
            previous = previous->next;
        }
        previous->next = menu_arrangement->next;
        free(menu_arrangement);
    }
    gtk_list_store_remove(list_arrangement_detail, &treeiter);
}

//  教学楼信息界面右键菜单显示教室详情预处理
int handle_Pop_up_menu_building(struct Pop_up_menu_building *pop_menu, GdkEvent *event)
{
    if (fp == NULL)
    {
        return FALSE;
    }

    g_return_val_if_fail(pop_menu != NULL, FALSE);
    g_return_val_if_fail(GTK_IS_MENU(pop_menu->menu), FALSE);
    g_return_val_if_fail(event != NULL, FALSE);

    if (event->type == GDK_BUTTON_PRESS)
    {
        GtkMenu *menu;
        GdkEventButton *button;
        button = (GdkEventButton *)event;
        GtkTreePath *path;
        if(gtk_tree_view_is_blank_at_pos(GTK_TREE_VIEW(pop_menu->treeview), button->x, button->y, &path, NULL, NULL, NULL))
        {
            return FALSE;
        }
        gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(pop_menu->treeview), button->x, button->y, &path, NULL, NULL, NULL);
        gtk_tree_model_get_iter(gtk_tree_view_get_model(pop_menu->treeview), &treeiter, path);
        gtk_tree_selection_select_iter(pop_menu->selection, &treeiter);
        gtk_tree_model_get(gtk_tree_view_get_model(pop_menu->treeview), &treeiter, 0, pop_menu->active, -1);
        menu = GTK_MENU(pop_menu->menu);
        if (button->button == GDK_BUTTON_SECONDARY)
        {
            gtk_menu_popup(menu, NULL, NULL, NULL, NULL, button->button, button->time);
            return TRUE;
        }
    }
}

//  教室信息界面右键菜单显示排课详情预处理
int handle_Pop_up_menu_classroom(struct Pop_up_menu_classroom *pop_menu, GdkEvent *event)
{
    if (fp == NULL)
    {
        return FALSE;
    }

    g_return_val_if_fail(pop_menu != NULL, FALSE);
    g_return_val_if_fail(GTK_IS_MENU(pop_menu->menu), FALSE);
    g_return_val_if_fail(event != NULL, FALSE);

    if (event->type == GDK_BUTTON_PRESS)
    {
        GtkMenu *menu;
        GdkEventButton *button;
        button = (GdkEventButton *)event;
        GtkTreePath *path;
        if(gtk_tree_view_is_blank_at_pos(GTK_TREE_VIEW(pop_menu->treeview), button->x, button->y, &path, NULL, NULL, NULL))
        {
            return FALSE;
        }
        gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(pop_menu->treeview), button->x, button->y, &path, NULL, NULL, NULL);
        gtk_tree_model_get_iter(gtk_tree_view_get_model(pop_menu->treeview), &treeiter, path);
        gtk_tree_selection_select_iter(pop_menu->selection, &treeiter);
        gtk_tree_model_get(gtk_tree_view_get_model(pop_menu->treeview), &treeiter, 0, pop_menu->active, -1);
        menu = GTK_MENU(pop_menu->menu);
        if (button->button == GDK_BUTTON_SECONDARY)
        {
            gtk_menu_popup(menu, NULL, NULL, NULL, NULL, button->button, button->time);
            return TRUE;
        }
    }
}

//  显示程序名、版本、网址、作者等基本信息
void help_about(void)
{
    GtkWidget *about_win = gtk_about_dialog_new();
    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about_win), version);
    gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(about_win), name);
    gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about_win), url);
    gtk_about_dialog_set_website_label(GTK_ABOUT_DIALOG(about_win), url);
    gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(about_win), author);
    gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(about_win), NULL);
    gtk_dialog_run(GTK_DIALOG(about_win));
    gtk_widget_destroy(about_win);
}

//  显示帮助信息
void help(void)
{
    errmesg("自己尽情尝试吧～");
}

//  弹出错误信息提示窗口
void errmesg(const char *error)
{
    GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, error);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

//  撤销窗口
void destroy_window(GtkWidget *ignored, GtkWidget *window)
{
    gtk_widget_destroy(window);
}




/**************************
**		数据操作函数       **
**************************/
//  向 gtktree 中插入教学楼数据
void list_building_insert(GtkListStore *list, Building *building)
{
     gtk_list_store_insert_with_values(list, &treeiter, 0,
                                       POINTER, (gpointer)building,
                                       NUMBER, building->number,
                                       NAME, building->name,
                                       PHONE, building->phone,
                                       CLASSES, building->number_classrooms,
                                       STUDENTS, building->students,
                                       SQUARE, building->square,
                                       -1);
}

//  向 gtktree 中插入教室数据
void list_classroom_insert(GtkListStore *list, char *building_number, Classroom *classroom)
{
    gtk_list_store_insert_with_values(list, &treeiter, 0,
                                        POINTER_CLASSROOM, (gpointer)classroom,
                                        NUMBER_BUILDING, building_number,
                                        NUMBER_CLASSROOM, classroom->number,
                                        FLOOR, classroom->floor,
                                        SQUARE_CLASSROOM, classroom->square,
                                        STUDENTS_CLASSROOM, classroom->students,
                                        MEDIA, classroom->has_media ? "有" : "无",
                                        -1);
}

//  向 gtktree 中插入排课数据
void list_arrangement_insert(GtkListStore *list, char *building_number, char *classroom_number, Arrangement *arrangement)
{
    gtk_list_store_insert_with_values(list, &treeiter, 0,
                                        POINTER_ARRANGEMENT, (gpointer)arrangement,
                                        NUMBER_BUILDING_ARRANGEMENT, building_number,
                                        NUMBER_CLASSROOM_ARRANGEMENT, classroom_number,
                                        TERM, arrangement->term,
                                        WEEK_OF_TERM, arrangement->weeks_of_term,
                                        WEEKDAY, arrangement->weekday,
                                        TIME_OF_DAY, arrangement->time_of_day,
                                        NAME_ARRANGEMENT, arrangement->name,
                                        STUDENTS_ARRANGEMENT, arrangement->students,
                                        COLLEDGE, arrangement->colledge,
                                        -1);
}

//  搜索教学楼信息预处理
void handle_search_building_data(GtkWidget *ignored, struct Building_Entry * data)
{
    char name[10];
    char number[10];
    strcpy(name, gtk_entry_get_text(GTK_ENTRY(data->entry_name)));
    strcpy(number, gtk_entry_get_text(GTK_ENTRY(data->entry_number)));
    free(data);
    search_building_detail(number, name);
}

//  搜索教室信息预处理
void handle_search_classroom_data(GtkWidget *ignored, struct Classroom_Entry *data)
{
    Classroom * temp_classroom = (Classroom *)malloc(sizeof(Classroom));
    strcpy(temp_classroom->number, gtk_entry_get_text(GTK_ENTRY(data->entry_number)));
    temp_classroom->floor = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_floor)));
    temp_classroom->square = atof(gtk_entry_get_text(GTK_ENTRY(data->entry_square)));
    temp_classroom->students = atof(gtk_entry_get_text(GTK_ENTRY(data->entry_students)));
    temp_classroom->has_media = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->choice));

    GtkTreeIter iter;
    gtk_combo_box_get_active_iter(GTK_COMBO_BOX(data->combo), &iter);
    gchar *number;
    gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(data->combo)), &iter, 0, &number, -1);

    search_classroom_detail(temp_classroom, number);

    free(data);
}

//  搜索排课信息预处理
void handle_search_arrangement_data(GtkWidget *ignored, struct Arrangement_Entry *data)
{
    Arrangement * temp_arrangement = (Arrangement *)malloc(sizeof(Arrangement));
    strcpy(temp_arrangement->term, gtk_entry_get_text(GTK_ENTRY(data->entry_term)));
    strcpy(temp_arrangement->weeks_of_term, gtk_entry_get_text(GTK_ENTRY(data->entry_weeks_of_term)));
    strcpy(temp_arrangement->time_of_day, gtk_entry_get_text(GTK_ENTRY(data->entry_time_of_day)));
    strcpy(temp_arrangement->name, gtk_entry_get_text(GTK_ENTRY(data->entry_name)));
    strcpy(temp_arrangement->colledge, gtk_entry_get_text(GTK_ENTRY(data->entry_colledge)));
    temp_arrangement->weekday = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_weekday)));

    char number_building[10];
    strcpy(number_building, gtk_entry_get_text(GTK_ENTRY(data->entry_building_number)));
    char number_classroom[10];
    strcpy(number_classroom, gtk_entry_get_text(GTK_ENTRY(data->entry_classroom_number)));

    search_arrangement_detail(temp_arrangement, number_building, number_classroom);

    free(data);
}

//  统计二功能实现
void count_2_handle_data(GtkWidget *ignored, GtkEntry *entry_term)
{
    GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
    gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[1]);
    gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_count);
    g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_count), vbox);

    GtkListStore *list;
    list = gtk_list_store_new(6, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_1 = gtk_tree_view_column_new_with_attributes(count_2_string[0], render, "text", 1, NULL);
    GtkTreeViewColumn *col_2 = gtk_tree_view_column_new_with_attributes(count_2_string[1], render, "text", 2, NULL);
    GtkTreeViewColumn *col_3 = gtk_tree_view_column_new_with_attributes(count_2_string[2], render, "text", 3, NULL);
    GtkTreeViewColumn *col_4 = gtk_tree_view_column_new_with_attributes(count_2_string[3], render, "text", 4, NULL);
    GtkTreeViewColumn *col_5 = gtk_tree_view_column_new_with_attributes(count_2_string[4], render, "text", 5, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_1);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_2);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_3);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_4);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_5);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);

    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_count);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

    const char *term = gtk_entry_get_text(entry_term);

    //  统计某学期各教学楼开课情况
    int classes;
    int hours;
    int students;
    int colledges;

    Building *current_building = head;
    Classroom *current_classroom = current_building->classroom;
    Classroom *temp_classroom = NULL;
    Arrangement *current_arrangement = current_classroom->arrangement;
    Arrangement *temp_arrangement = NULL;

    while (current_building != NULL)
    {
        classes = 0;
        hours = 0;
        students = 0;
        colledges = 0;
        current_classroom = current_building->classroom;
        while (current_classroom != NULL)
        {
            current_arrangement = current_classroom->arrangement;
            while (current_arrangement != NULL)
            {
                if (strcmp(current_arrangement->term, term) == 0)
                {
                    temp_classroom = current_building->classroom;
                    temp_arrangement = NULL;
                    int flag = 1;
                    while (flag == 1 && temp_arrangement != current_arrangement)
                    {
                        while (flag == 1 && temp_arrangement != NULL && temp_arrangement != current_arrangement)
                        {
                            if (strcmp(current_arrangement->colledge, temp_arrangement->colledge) == 0)
                            {
                                flag = 0;
                            }
                            temp_arrangement = temp_arrangement->next;
                        }
                        temp_classroom = temp_classroom->next;
                        if (temp_classroom != NULL)
                        {
                            temp_arrangement = temp_classroom->arrangement;
                        }
                    }
                    if (flag == 1)
                    {
                        colledges += 1;
                    }
                    classes += 1;
                    hours += 2;
                    students += current_arrangement->students;
                }
                current_arrangement = current_arrangement->next;
            }
            current_classroom = current_classroom->next;
        }
        gtk_list_store_insert_with_values(list, &treeiter, 9,
                                            0, (gpointer)current_building,
                                            1, current_building->name,
                                            2, classes,
                                            3, hours,
                                            4, students,
                                            5, colledges,
                                            -1);
        current_building = current_building->next;
    }
    gtk_widget_show_all(win_count);
}

//  统计三功能实现
void count_3_handle_data(GtkWidget *ignored, struct Count_3_ENTRY *data)
{
    GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
    gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[1]);
    gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_count);
    g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_count), vbox);

    GtkListStore *list;
    list = gtk_list_store_new(8, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_1 = gtk_tree_view_column_new_with_attributes(count_3_string[0], render, "text", 1, NULL);
    GtkTreeViewColumn *col_2 = gtk_tree_view_column_new_with_attributes(count_3_string[1], render, "text", 2, NULL);
    GtkTreeViewColumn *col_3 = gtk_tree_view_column_new_with_attributes(count_3_string[2], render, "text", 3, NULL);
    GtkTreeViewColumn *col_4 = gtk_tree_view_column_new_with_attributes(count_3_string[3], render, "text", 4, NULL);
    GtkTreeViewColumn *col_5 = gtk_tree_view_column_new_with_attributes(count_3_string[4], render, "text", 5, NULL);
    GtkTreeViewColumn *col_6 = gtk_tree_view_column_new_with_attributes(count_3_string[5], render, "text", 5, NULL);
    GtkTreeViewColumn *col_7 = gtk_tree_view_column_new_with_attributes(count_3_string[6], render, "text", 5, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_1);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_2);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_3);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_4);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_5);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_6);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_7);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);

    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_count);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

    //  统计某学期、某周的星期几、某教学楼，各教室各节次开课情况
    const char *term = gtk_entry_get_text(GTK_ENTRY(data->entry_term));
    char *week = gtk_entry_get_text(GTK_ENTRY(data->entry_week));
    int weekday = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_weekday)));
    const char *number = gtk_entry_get_text(GTK_ENTRY(data->entry_number));

    if (strcmp(term, "") == 0
        || strcmp(week, "") == 0
        || strcmp(number, "") == 0
        || strcmp(gtk_entry_get_text(GTK_ENTRY(data->entry_weekday)), "") == 0)
    {
        errmesg("统计时请填写全部信息！");
        return ;
    }

    char name[6][30];
    Building *current_building = head;
    while (strcmp(current_building->number, number) != 0)
    {
        current_building = current_building->next;
    }

    Classroom *current_classroom = current_building->classroom;
    Arrangement *current_arrangement = NULL;
    int i;
    while (current_classroom != NULL)
    {
        for (i = 0; i < 6; i++)
        {
            strcpy(name[i], "自习");
        }
        current_arrangement = current_classroom->arrangement;
        while (current_arrangement != NULL)
        {
            if (strcmp(current_arrangement->term, term) == 0
                && in_range(week, current_arrangement->weeks_of_term) == 1
                && current_arrangement->weekday == weekday)
            {
                if (current_arrangement->time_of_day[2] == '1')
                {
                    i = 5;
                }
                else if (current_arrangement->time_of_day[2] == '-')
                {
                    i = 6;
                }
                else
                {
                    i = (current_arrangement->time_of_day[2] - '0') / 2;
                }
                strcpy(name[i-1], current_arrangement->name);
            }
            current_arrangement = current_arrangement->next;
        }
        gtk_list_store_insert_with_values(list, &treeiter, 0,
                                            0, (gpointer)current_classroom,
                                            1, current_classroom->number,
                                            2, name[0],
                                            3, name[1],
                                            4, name[2],
                                            5, name[3],
                                            6, name[4],
                                            7, name[5],
                                            -1);
        current_classroom = current_classroom->next;
    }
    gtk_widget_show_all(win_count);
}

//  统计四功能实现
void count_4_handle_data(GtkWidget *ignored, struct Count_4_ENTRY *data)
{
    GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
    gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[1]);
    gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_count);
    g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_count), vbox);

    GtkListStore *list;
    list = gtk_list_store_new(7, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_1 = gtk_tree_view_column_new_with_attributes(count_4_string[0], render, "text", 1, NULL);
    GtkTreeViewColumn *col_2 = gtk_tree_view_column_new_with_attributes(count_4_string[1], render, "text", 2, NULL);
    GtkTreeViewColumn *col_3 = gtk_tree_view_column_new_with_attributes(count_4_string[2], render, "text", 3, NULL);
    GtkTreeViewColumn *col_4 = gtk_tree_view_column_new_with_attributes(count_4_string[3], render, "text", 4, NULL);
    GtkTreeViewColumn *col_5 = gtk_tree_view_column_new_with_attributes(count_4_string[4], render, "text", 5, NULL);
    GtkTreeViewColumn *col_6 = gtk_tree_view_column_new_with_attributes(count_4_string[5], render, "text", 6, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_1);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_2);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_3);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_4);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_5);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_6);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);

    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_count);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

    //  显示某学期、某学院各门课程在各教学楼开课情况
    const char* term = gtk_entry_get_text(GTK_ENTRY(data->entry_term));
    const char* colledge = gtk_entry_get_text(GTK_ENTRY(data->entry_colledge));

    Building *current_building = head;
    Classroom *current_classroom = NULL;
    Arrangement *current_arrangement = NULL;
    while (current_building != NULL)
    {
        current_classroom = current_building->classroom;
        while (current_classroom != NULL)
        {
            current_arrangement = current_classroom->arrangement;
            while (current_arrangement != NULL)
            {
                if (strcmp(current_arrangement->term, term) == 0
                    && strcmp(current_arrangement->colledge, colledge) == 0)
                {
                    gtk_list_store_insert_with_values(list, &treeiter, 0,
                                                        0, (gpointer)current_arrangement,
                                                        1, current_arrangement->name,
                                                        2, current_building->name,
                                                        3, current_classroom->number,
                                                        4, current_arrangement->weeks_of_term,
                                                        5, current_arrangement->weekday,
                                                        6, current_arrangement->time_of_day,
                                                        -1);
                }
                current_arrangement = current_arrangement->next;
            }
            current_classroom = current_classroom->next;
        }
        current_building = current_building->next;
    }

    gtk_widget_show_all(win_count);
}

//  统计五功能实现
void count_5_handle_data(GtkWidget *ignored, struct Count_5_ENTRY *data)
{
    GtkWidget *win_count = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win_count), 0);
    gtk_window_set_title(GTK_WINDOW(win_count), count_win_titles_string[1]);
    gtk_window_set_position(GTK_WINDOW(win_count), GTK_WIN_POS_CENTER);
    gtk_widget_realize(win_count);
    g_signal_connect(win_count, "destroy", G_CALLBACK(destroy_window), win_count);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(win_count), vbox);

    GtkListStore *list;
    list = gtk_list_store_new(7, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list));
    GtkCellRenderer *render = gtk_cell_renderer_text_new();
    g_object_set(G_OBJECT(render), "foreground", "black", NULL);

    GtkTreeViewColumn *col_1 = gtk_tree_view_column_new_with_attributes(count_5_string[0], render, "text", 1, NULL);
    GtkTreeViewColumn *col_2 = gtk_tree_view_column_new_with_attributes(count_5_string[1], render, "text", 2, NULL);
    GtkTreeViewColumn *col_3 = gtk_tree_view_column_new_with_attributes(count_5_string[2], render, "text", 3, NULL);
    GtkTreeViewColumn *col_4 = gtk_tree_view_column_new_with_attributes(count_5_string[3], render, "text", 4, NULL);
    GtkTreeViewColumn *col_5 = gtk_tree_view_column_new_with_attributes(count_5_string[4], render, "text", 5, NULL);
    GtkTreeViewColumn *col_6 = gtk_tree_view_column_new_with_attributes(count_5_string[5], render, "text", 6, NULL);

    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_1);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_2);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_3);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_4);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_5);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col_6);

    gtk_box_pack_start(GTK_BOX(vbox), treeview, TRUE, TRUE, 8);

    GtkWidget *button = gtk_button_new_with_label("关闭");
    g_signal_connect(button, "clicked", G_CALLBACK(destroy_window), (gpointer)win_count);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 8);

    //  显示某学期、某课程在各教学楼开课情况
    const char* term = gtk_entry_get_text(GTK_ENTRY(data->entry_term));
    const char* name = gtk_entry_get_text(GTK_ENTRY(data->entry_arrangement));

    Building *current_building = head;
    Classroom *current_classroom = NULL;
    Arrangement *current_arrangement = NULL;
    while (current_building != NULL)
    {
        current_classroom = current_building->classroom;
        while (current_classroom != NULL)
        {
            current_arrangement =  current_classroom->arrangement;
            while (current_arrangement != NULL)
            {
                if (strcmp(current_arrangement->term, term) == 0
                    && strcmp(current_arrangement->name, name) == 0)
                {
                    gtk_list_store_insert_with_values(list, &treeiter, 0,
                                                        0, (gpointer)current_arrangement,
                                                        1, current_building->name,
                                                        2, current_classroom->number,
                                                        3, current_arrangement->colledge,
                                                        4, current_arrangement->weeks_of_term,
                                                        5, current_arrangement->weekday,
                                                        6, current_arrangement->time_of_day,
                                                        -1);
                    printf("%s\n", current_arrangement->time_of_day);
                }
                current_arrangement = current_arrangement->next;
            }
            current_classroom = current_classroom->next;
        }
        current_building = current_building->next;
    }

    gtk_widget_show_all(win_count);
}

//  新建教学楼信息预处理
void handle_new_building(GtkWidget *ignored, struct Building_Entry * data)
{
    Building *building_current = (Building *)malloc(sizeof(Building));
    strcpy(building_current->number, gtk_entry_get_text(GTK_ENTRY(data->entry_number)));
    strcpy(building_current->name, gtk_entry_get_text(GTK_ENTRY(data->entry_name)));
    strcpy(building_current->phone, gtk_entry_get_text(GTK_ENTRY(data->entry_phone)));
    building_current->number_classrooms = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_classes)));
    building_current->students = 0;
    building_current->square = 0;
    building_current->classroom = NULL;
    building_current->next = head;
    head = building_current;
    free(data);
    list_building_insert(list_building, building_current);
}

//  新建教室信息预处理
void handle_new_classroom(GtkWidget *ignored, struct Classroom_Entry * data)
{
    Classroom * temp_classroom = (Classroom *)malloc(sizeof(Classroom));
    strcpy(temp_classroom->number, gtk_entry_get_text(GTK_ENTRY(data->entry_number)));
    temp_classroom->floor = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_floor)));
    temp_classroom->square = atof(gtk_entry_get_text(GTK_ENTRY(data->entry_square)));
    temp_classroom->students = atof(gtk_entry_get_text(GTK_ENTRY(data->entry_students)));
    temp_classroom->has_media = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->choice));
    temp_classroom->arrangement = NULL;

    GtkTreeIter iter;
    gtk_combo_box_get_active_iter(GTK_COMBO_BOX(data->combo), &iter);
    gchar *number;
    gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(data->combo)), &iter, 0, &number, -1);
    Building * temp_building = search_building_by_number(number);
    temp_building->students += temp_classroom->students;
    temp_building->square += temp_classroom->square;
    temp_classroom->next = temp_building->classroom;
    temp_building->classroom = temp_classroom;
    free(data);
}

//  新建排课信息预处理
void handle_new_arrangement(GtkWidget *ignored, struct Arrangement_Entry * data)
{
    Arrangement * temp_arrangement = (Arrangement *)malloc(sizeof(Arrangement));
    strcpy(temp_arrangement->term, gtk_entry_get_text(GTK_ENTRY(data->entry_term)));
    strcpy(temp_arrangement->weeks_of_term, gtk_entry_get_text(GTK_ENTRY(data->entry_weeks_of_term)));
    strcpy(temp_arrangement->time_of_day, gtk_entry_get_text(GTK_ENTRY(data->entry_time_of_day)));
    strcpy(temp_arrangement->name, gtk_entry_get_text(GTK_ENTRY(data->entry_name)));
    strcpy(temp_arrangement->colledge, gtk_entry_get_text(GTK_ENTRY(data->entry_colledge)));
    temp_arrangement->weekday = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_weekday)));
    temp_arrangement->students = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_students)));

    GtkTreeIter iter;
    gtk_combo_box_get_active_iter(GTK_COMBO_BOX(data->combo), &iter);
    gchar *number;
    gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(data->combo)), &iter, 0, &number, -1);
    gchar classroom_number[10];
    strcpy(classroom_number, gtk_entry_get_text(GTK_ENTRY(data->entry_classroom_number)));
    Building *temp_building = search_building_by_number(number);
    Classroom *temp_classroom = search_classroom_by_number(temp_building, classroom_number);
    if (temp_classroom == NULL)
    {
        errmesg("请输入正确的教室编号！");
    }
    else
    {
        temp_arrangement->next = temp_classroom->arrangement;
        temp_classroom->arrangement = temp_arrangement;
    }
    free(data);
}

//  修改教学楼信息预处理
void handle_change_building(GtkWidget *ignored, struct Building_Entry *data)
{
    strcpy(menu_building->number, gtk_entry_get_text(GTK_ENTRY(data->entry_number)));
    strcpy(menu_building->name, gtk_entry_get_text(GTK_ENTRY(data->entry_name)));
    strcpy(menu_building->phone, gtk_entry_get_text(GTK_ENTRY(data->entry_phone)));
    menu_building->number_classrooms = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_classes)));
    free(data);

    gtk_list_store_remove(list_building, &treeiter);
    list_building_insert(list_building, menu_building);
}

//  修改教室信息预处理
void handle_change_classroon(GtkWidget *ignored, struct Classroom_Entry *data)
{
    menu_building->students -= menu_classroom->students;
    menu_building->square -= menu_classroom->square;

    strcpy(menu_classroom->number, gtk_entry_get_text(GTK_ENTRY(data->entry_number)));
    menu_classroom->floor = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_floor)));
    menu_classroom->square = atof(gtk_entry_get_text(GTK_ENTRY(data->entry_square)));
    menu_classroom->students = atof(gtk_entry_get_text(GTK_ENTRY(data->entry_students)));
    menu_classroom->has_media = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->choice));

    menu_building->students += menu_classroom->students;
    menu_building->square += menu_classroom->square;
    free(data);

    gtk_list_store_remove(list_classroom_detail, &treeiter);
    list_classroom_insert(list_classroom_detail, menu_building->number, menu_classroom);
    gtk_window_reshow_with_initial_size((GtkWindow *)win_classroom);
}

//  修改排课信息预处理
void handle_change_arrangement(GtkWidget *ignored, struct Arrangement_Entry *data)
{
    strcpy(menu_arrangement->term, gtk_entry_get_text(GTK_ENTRY(data->entry_term)));
    strcpy(menu_arrangement->weeks_of_term, gtk_entry_get_text(GTK_ENTRY(data->entry_weeks_of_term)));
    strcpy(menu_arrangement->time_of_day, gtk_entry_get_text(GTK_ENTRY(data->entry_time_of_day)));
    strcpy(menu_arrangement->name, gtk_entry_get_text(GTK_ENTRY(data->entry_name)));
    strcpy(menu_arrangement->colledge, gtk_entry_get_text(GTK_ENTRY(data->entry_colledge)));
    menu_arrangement->weekday = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_weekday)));
    menu_arrangement->students = atoi(gtk_entry_get_text(GTK_ENTRY(data->entry_students)));
    free(data);

    gtk_list_store_remove(list_arrangement_detail, &treeiter);
    list_arrangement_insert(list_arrangement_detail, menu_building->number, menu_classroom->number, menu_arrangement);
    gtk_window_reshow_with_initial_size((GtkWindow *)win_arrangement);
}

//  通过编号搜索教学楼
Building * search_building_by_number(gchar * number)
{
    Building * aim = head;
    while (aim != NULL && strcmp(number, aim->number) != 0)
    {
        aim = aim->next;
    }
    return aim;
}

//  通过编号搜索教室
Classroom * search_classroom_by_number(Building *building, char *number)
{
    Classroom * aim = building->classroom;
    while (aim != NULL && strcmp(number, aim->number) != 0)
    {
        aim = aim->next;
    }
    return aim;
}

//  精确搜索教学楼
void search_building_detail(char *number, char *name)
{
    Building *aim = head;
    if (aim == NULL)
    {
        errmesg("请先录入信息后再查询！");
    }
    else if (strcmp(number, "") != 0)
    {
       aim = search_building_by_number(number);
       if (aim == NULL)
       {
            errmesg("未找到相关教学楼！");
            return ;
       }
       else if (strstr(aim->name, name) != NULL)
       {
            show_building_detail();
            list_building_insert(list_building_detail, aim);
            gtk_widget_show_all(win_building);
       }
    }
    else if (strcmp(name, "") != 0)
    {
        int flag = 0;
        while(aim != NULL)
        {
            if (strstr(aim->name, name) != NULL)
            {
                if (flag == 0)
                {
                    show_building_detail();
                    flag = 1;
                }
                list_building_insert(list_building_detail, aim);
            }
            aim = aim->next;
        }
        if(flag == 0)
        {
            errmesg("未找到相关教学楼！");
        }
        else
        {
            gtk_widget_show_all(win_building);
        }
    }
    else
    {
        errmesg("请输入信息后再查询！");
    }
}

//  精确搜索教室
void search_classroom_detail(Classroom *classroom, char *building_number)
{
    if (head == NULL)
    {
        errmesg("请录入信息后再查询！");
        return ;
    }

    Building *building = search_building_by_number(building_number);
    if (strcmp(classroom->number, "") != 0)
    {
        Classroom *aim = search_classroom_by_number(building, classroom->number);
        if (aim == NULL)
        {
            errmesg("未找到相关教室！");
        }
        else
        {
            if (classroom->floor != 0)
            {
                if (classroom->floor != aim->floor)
                {
                    aim = NULL;
                }
            }
            if (classroom->square != 0 && aim != NULL)
            {
                if (classroom->square != aim->square)
                {
                    aim = NULL;
                }
            }
            if (classroom->students != 0 && aim != NULL)
            {
                if (classroom->students != aim->students)
                {
                    aim = NULL;
                }
            }
            if (aim != NULL && classroom->has_media != aim->has_media)
            {
                aim = NULL;
            }

            if (aim != NULL)
            {
                show_classroom_detail(0);
                list_classroom_insert(list_classroom_detail, building_number, aim);
                gtk_widget_show_all(win_classroom);
            }
            else
            {
                errmesg("未找到相关教室！");
            }
        }
    }
    else
    {
        int flag = 0;
        Classroom *temp = building->classroom;
        Classroom *aim = temp;
        while(temp != NULL)
        {
            if (classroom->floor != 0)
            {
                while(aim != NULL && classroom->floor != aim->floor)
                {
                    aim = aim->next;
                }
            }
            if (classroom->square != 0 && aim != NULL)
            {
                while(aim != NULL && classroom->square != aim->square)
                {
                    aim = aim->next;
                }
            }
            if (classroom->students != 0 && aim != NULL)
            {
                while(aim != NULL && classroom->students != aim->students)
                {
                    aim = aim->next;
                }
            }
            while(aim != NULL && classroom->has_media != aim->has_media)
            {
                aim = aim->next;
            }

            if (aim == temp)
            {
                  if (flag == 0)
                  {
                    flag = 1;
                    show_classroom_detail(0);
                  }
                  list_classroom_insert(list_classroom_detail, building_number, aim);
                  if (aim != NULL)
                  {
                    aim = aim->next;
                  }
            }
            temp = aim;
        }
        if (flag == 1)
        {
            gtk_widget_show_all(win_classroom);
        }
        else
        {
            errmesg("未找到相关教室！");
        }
    }
 }

//  精确搜索课程
void search_arrangement_detail(Arrangement * arrangement, char *number_building, char *number_classroom)
{
    if (head == NULL)
    {
        errmesg("请先录入信息后再查询！");
        return ;
    }

    Building *building = head;
    Classroom *classroom = NULL;
    Arrangement *temp = NULL;
    Arrangement *aim = NULL;
    int flag = 0;
    while (building != NULL)
    {
        if (strcmp(number_building, "") != 0)
        {
            while (building != NULL && strcmp(number_building, building->number) != 0)
            {
                building = building->next;
            }
        }

        if (building != NULL)
        {
            classroom = building->classroom;
        }
        while (classroom != NULL)
        {
            if (strcmp(number_classroom, "") != 0)
            {
                while (classroom != NULL && strcmp(number_classroom, classroom->number) != 0)
                {
                    classroom = classroom->next;
                }
            }
            if (classroom != NULL)
            {
                temp = classroom->arrangement;
                aim = temp;
            }
            while (temp != NULL)
            {
                if (strcmp(arrangement->term, "") != 0)
                {
                    while (aim != NULL && strcmp(arrangement->term, aim->term) != 0)
                    {
                        aim = aim->next;
                    }
                }
                if (aim != NULL && strcmp(arrangement->weeks_of_term, "") != 0)
                {
                    while (aim != NULL && in_range(arrangement->weeks_of_term, aim->weeks_of_term) != 1)
                    {
                        aim = aim->next;
                    }
                }
                while (aim != NULL && arrangement->weekday != 0 && arrangement->weekday != aim->weekday)
                {
                    aim = aim->next;
                }
                if (aim != NULL && strcmp(arrangement->time_of_day, "") != 0)
                {
                    while (aim != NULL && in_range(arrangement->time_of_day, aim->time_of_day) != 1)
                    {
                        aim = aim->next;
                    }
                }
                if (aim != NULL && strcmp(arrangement->name, "") != 0)
                {
                    while (aim != NULL && strcmp(arrangement->name, aim->name) != 0)
                    {
                        aim = aim->next;
                    }
                }
                if (aim != NULL && strcmp(arrangement->colledge, "") != 0)
                {
                    while (aim != NULL && strcmp(arrangement->colledge, aim->colledge) != 0)
                    {
                        aim = aim->next;
                    }
                }
                if (aim == temp)
                {
                    if (flag == 0)
                    {
                        show_arrangement_detail(0);
                        flag = 1;
                    }
                    list_arrangement_insert(list_arrangement_detail, building->number, classroom->number, aim);
                    if (aim != NULL)
                    {
                        aim = aim->next;
                    }
                }
                temp = aim;
            }
            if (classroom != NULL)
            {
                classroom = classroom->next;
            }
        }
        if (building != NULL)
        {
            building = building->next;
        }
    }
    if (flag == 1)
    {
        gtk_widget_show_all(win_arrangement);
    }
    else
    {
        errmesg("未找到相关课程！");
    }
}

//  将“xx-xx" 字符串分割
void split_time_string(char *time, int min_max[2])
{
    int i;
    for (i = 0; time[i] != '\0' && time[i] != '-'; i++ );
    if (time[i] == '-')
    {
        char temp = time[i];
        time[i] = '\0';
        min_max[0] = atoi(time);
        min_max[1] = atoi(time + i + 1);
        time[i] = temp;
    }
    else
    {
        min_max[0] = min_max[1] = atoi(time);
    }
}

//  判断课程时间是否符合
int in_range(char *time1, char *time2)
{
    int min_max1[2], min_max2[2];
    split_time_string(time1, min_max1);
    split_time_string(time2, min_max2);
    if (min_max2[0] <= min_max1[0] && min_max1[1] <= min_max2[1])
    {
        return 1;
    }
    else
    {
        return 0;
    }
}




/**************************
**		文件操作函数       **
**************************/
//  新建文件
void file_new(void)
{
    char *file_name = choose_file(SAVE_FILE);
    if (file_name != NULL)
    {
        file_close();
        remove(file_name);
        fp = fopen(file_name, "wb+");
        free(file_name);
        if (fp == NULL)
        {
            errmesg("文件创建失败");
        }
    }
}

//  打开文件
void file_open(void)
{
    if (fp != NULL)
    {
        gtk_list_store_clear(list_building);
        fclose(fp);
        fp = NULL;
    }
    char *file = choose_file(OPEN_FILE);
    if (file != NULL)
    {
        fp = fopen(file, "rb+");
        if (file == NULL)
        {
            errmesg("数据读取失败！");
        }
        else
        {
            head = (Building *)malloc(sizeof(Building));
            Building *building = head;
            fread(building, sizeof(Building), 1, fp);
            while ( 1 )
            {
                if (building->classroom != NULL)
                {
                    Classroom *classroom = (Classroom *)malloc(sizeof(Classroom));
                    building->classroom = classroom;
                    fread(classroom, sizeof(Classroom), 1, fp);
                    while ( 1 )
                    {
                        if (classroom->arrangement != NULL)
                       {
                            Arrangement *arrangement = (Arrangement *)malloc(sizeof(Arrangement));
                            classroom->arrangement = arrangement;
                            fread(arrangement, sizeof(Arrangement), 1, fp);
                            while (arrangement->next != NULL)
                            {
                                arrangement->next = (Arrangement *)malloc(sizeof(Arrangement));
                                fread(arrangement->next, sizeof(Arrangement), 1, fp);
                                arrangement = arrangement->next;
                            }
                        }

                        if (classroom->next == NULL)
                        {
                            break;
                        }
                        classroom->next = (Classroom *)malloc(sizeof(Classroom));
                        fread(classroom->next, sizeof(Classroom), 1, fp);
                        classroom = classroom->next;
                    }
                }

                list_building_insert(list_building, building);
                if (building->next == NULL)
                {
                    break;
                }
                building->next = (Building *)malloc(sizeof(Building));
                fread(building->next, sizeof(Building), 1, fp);
                building = building->next;
            }
            test_print_data();
        }
    }
    free(file);
 }

//  保存文件
void file_save(void)
{
   if (fp == NULL)
   {
        errmesg("请先打开数据文件！");
   }
   else
   {
        rewind(fp);
        save_data();
   }
}

//  文件另存为
void file_save_as(void)
{
    if (fp == NULL)
    {
        errmesg("请先打开数据文件！");
    }
    else
    {
        char *file = choose_file(SAVE_FILE_AS);
        if (file)
        {
            FILE *fp_temp = fp;
            remove(file);
            fp = fopen(file, "wb+");
            free(file);
            if (fp == NULL)
            {
                errmesg("文件打开失败！");
            }
            else
            {
                save_data();
            }
            fp = fp_temp;
        }
    }
}

//  关闭文件
void file_close(void)
{
    if (fp != NULL)
    {
        gtk_list_store_clear(list_building);
        fclose(fp);
        fp = NULL;
    }
}

//   写入文件
void save_data(void)
{
    Building *building = head;
    while (building != NULL)
    {
        fwrite(building, sizeof(Building), 1, fp);
        printf("building %s saved\n", building->number);
        Classroom *classroom = building->classroom;
        while (classroom != NULL)
        {
            fwrite(classroom, sizeof(Classroom), 1, fp);
            printf("\tclassroom %s saved\n", classroom->number);
            Arrangement *arrangement = classroom->arrangement;
            while (arrangement != NULL)
            {
                fwrite(arrangement, sizeof(Arrangement), 1, fp);
                printf("\t\tarrangement %s saved\n", arrangement->name);
                arrangement = arrangement->next;
            }
            classroom = classroom->next;
        }
        building = building->next;
    }
}




/**************************
**		测试输出函数       **
**************************/
//  读取数据时输出数据
void test_print_data(void)
{
    Building *building = head;
    printf("Begin to load data ...\n\n");
    while (building != NULL)
    {
        printf("building number    %s\n", building->number);
        Classroom *classroom = building->classroom;
        while(classroom != NULL)
        {
            printf("\tclassroom number    %s\n", classroom->number);
            Arrangement *arrangement = classroom->arrangement;
            while(arrangement != NULL)
            {
                printf("\t\tarrangement name    %s\n", arrangement->name);
                arrangement = arrangement->next;
            }
            classroom = classroom->next;
        }
        building = building->next;
    }
    printf("\nloading ended.\n\n");
}
